<?php

require_once('facebook.php');


$FBUserData = null;

function LoadFacebookUserData()

{

 $FBUserData = null;

 $facebook = new Facebook(array(

    'appId'  => FACEBOOK_APP_ID,

    'secret' => FACEBOOK_APP_SECRET,

    'cookie' => true

 ));

   

 $fbUser = $facebook->getUser();
 var_dump($fbUser);

 //$logger = GetLogger(LOGS_PATH . 'facebook.txt');

 if ($fbUser)

 {


     $FBUserData = $facebook->api('/me');

 }

   

 return $FBUserData;

}

function GetCurrentFacebookUserData()

{

 global $FBUserData;

 // Check if data is already loaded

 If (!is_null($FBUserData))

    return $FBUserData;

 $FBUserData = LoadFacebookUserData();

 return $FBUserData;

}



/**

* Initializes user session by creating global objects: $USER_SESSION and $USER.

* @global User $FACEBOOK_USER

*/

function InitializeFacebookUserSession()

{
 global $FACEBOOK_USER, $FACEBOOK;

 // Create our Application instance (replace this with your appId and secret).

 $FACEBOOK = new Facebook(array(

    'appId'  => FACEBOOK_APP_ID,

    'secret' => FACEBOOK_APP_SECRET,

    'cookie' => true,

 ));

 $fbUser = $FACEBOOK->getUser();

 $FACEBOOK_USER = null;

 $logger = GetLogger(LOGS_PATH . 'facebook.txt');

 if ($fbUser)

 {

    try

    {

     $FACEBOOK_USER = $FACEBOOK->api('/me');

    }

    catch (FacebookApiException $e)

    {

     $logger->LogError("FacebookApiException on authentication: " . json_encode($e));

    }

 }

 else

    $logger->LogError('Authentication error, $facebook->getUser returned null.');

}

function GetFacebookInstance()

{

 global $FACEBOOK;

 if (!$FACEBOOK)

    InitializeFacebookUserSession();

 return $FACEBOOK;

}

/**

* Gets current user if already set and loads it from database if not.

* @global User $FACEBOOK_USER

* @return User

*/

function GetCurrentFacebookUser()

{

 global $FACEBOOK_USER;

 if (!$FACEBOOK_USER)

    InitializeFacebookUserSession();

 return $FACEBOOK_USER;

}

/**

* Checks that the current user is logged in.

* @return User

*/

function FacebookUserIsLoggedIn()

{

 return (GetCurrentFacebookUser() != null);

}


function GetFacebookPermissionsPageRedirectUrl($redirect)

{

 $url = 'http://www.facebook.com/connect/uiserver.php?app_id=' . FACEBOOK_APP_ID

       . '&next=' . $redirect . '&display=page&perms=&return_session=1&session_version=3'

       . '&fbconnect=0&canvas=1&legacy_return=1&method=permissions.request';

 return $url;

}

function RedirectIframesParentPage($url)

{

 ?>

<a target="_parent" href="<?php print mb_htmlspecialchars($url);?>" id="redirect-form"></a>

<script type="text/javascript">

<?php /* either access window.top.location.href directly */?>

try {

 window.top.location.href = '<?php print $url;?>';

}

catch(e){}

<?php /* or try to trigger a click on the anchor */?>

fireEvent(document.getElementById('redirect-form'), 'click');

function fireEvent(element, event)

{

 if (document.createEvent)

 {

  // dispatch for firefox + others

  var evt = document.createEvent("HTMLEvents");

  evt.initEvent(event, true, true); // event type, bubbling, cancelable

  return !element.dispatchEvent(evt);

 }

 else if (element.fireEvent)

 {

  // dispatch for IE

  var evt = document.createEventObject();

  return element.fireEvent('on' + event,evt)

 }

}

</script>

 <?php

 exit();

}

?>